package web

import (
	"fmt"
	"github.com/roseboy/goo"
	"time"
)

type DemoController struct {
	goo.Web
}

func (web *DemoController) Controller() {
	web.Intercept(DemoControllerInterceptor)

	web.Get("../../", func(ctx *goo.Context) {
		fmt.Println(ctx.GetHeader("Cookie"))
		ctx.SetHeader("OK", "ok233441")
		ctx.SetAttr("title", "Goo Demo Page - "+ctx.GetParaWithDefault("name", "Hello"))
		ctx.SetCookieVal("ooo", "123131")
		//ctx.RemoveCookie("ooo")
		ctx.SetSession(fmt.Sprintf("test%d", time.Now().Second()), "1324sdfweg")
		ctx.RenderHtml("demo/html/demo")
	}).Intercept(HomeInterceptor1, HomeInterceptor2, HomeInterceptor3)

	web.Get("hello/:name/:age", func(ctx *goo.Context) {
		ctx.RenderJsonStr("{\"name\":\"" + ctx.GetPara(":name") + "\"}")
	})

	web.Get("ok", func(ctx *goo.Context) {
		ctx.RenderText(fmt.Sprintf("Hello %s", ctx.GetPara("name")))
	})

	web.Get("test", test)
}

func test(ctx *goo.Context) {
	ctx.RenderText("111")
}

func DemoControllerInterceptor(inv *goo.Invocation) {
	fmt.Println("DemoController before")
	inv.Invoke()
	fmt.Println("DemoController after")
}

func HomeInterceptor1(inv *goo.Invocation) { //只给本方法注册拦截器
	fmt.Println("DemoController HOME before")
	inv.Invoke()
	fmt.Println("DemoController HOME after")
}
func HomeInterceptor2(inv *goo.Invocation) {
	fmt.Println("DemoController HOME2 before")
	inv.Invoke()
	fmt.Println("DemoController HOME2 after")
}
func HomeInterceptor3(inv *goo.Invocation) {
	fmt.Println("DemoController HOME3 before")
	inv.Invoke()
	fmt.Println("DemoController HOME3 after")
}
